/*/////////////////////////////////////////////////////////////////////////////
//           Version 23.08.2012
//
//  purposes:  Create a log file of parameters saved inside of spectra
//
//  Parameters saved:                       Explanation
//          FileNumber                   :     
//          StartTime                    : Time spectra began to accumulate
//          StopTime                     : Start time of the last added spectra in a .std file
//          SZA                          : Solar Zenith Angle           
//          Lat                          : Latitude
//          Long                         : Longitude
//          Altitude                     : Altitude
//          ElevationAngle               : ElevationAngle -- where the spectrometer *should* be looking
//          NumScans                     : Number of Scans added together
//          NumCounts_MotorOutOfPosition : How many cycles was the motor outside of acceptable range during this file
//          NumCounts_ExposureTimeReset  : How many times did the Exposure need to reset because it was too low/high  
//  
// 
//      Always update SpectraFolder, LogFileName by hand.
//
///////////////////////////////////////////////////////////////////////////////
// by Eric Gentry
// questions: Tilman.Hueneke@iup.uni-heidelberg.de
//            egentry@mit.edu
/////////////////////////////////////////////////////////////////////////////*/

import System;
import System.IO
      
    var LogFileName   = "Visible_RGB.txt"

//*****************************************************************************************// 

function main()
{

    var dispFileSystem = new ActiveXObject("Scripting.FileSystemObject");
    var outfile        = dispFileSystem.OpenTextFile(LogFileName,2, true);
    outfile.WriteLine("Wavelength\t" + "R\t" + "G\t" + "B\t" + "comp")
      
    var Calib0 = 417.297054494303
    var Calib1 = 0.129382466957868
    var Calib2 = -9.88020167566036E-06
    
    var R = 0
    var G = 0
    var B = 0
    var comp = 0
    
    var SSS = 0
    
    var w = 0 
    
    for(var count = 0; count < 2044; count++)
    {
        w = Calib0 + count*Calib1 + (count * count* Calib2)
        
        R = 0
        G = 0
        B = 0
        comp = 0
        
   if( w >= 380 && w < 440)
        {R = -(w - 440) / (440 - 350)
        G = 0.0
        B = 1.0 }
        
    else if( w >= 440 && w < 490)
        {R = 0
        G = (w - 440) / (490 - 440)
        B = 1 }
        
    else if (w >= 490 && w < 510)
        {R = 0.0
        G = 1.0
        B = -(w - 510) / (510 - 490)}
        
        
    else if(w >= 510 && w < 580)
        {R = (w - 510) / (580 - 510)
        G = 1.0
        B = 0.0   }
        
    else if (w >= 580 && w < 645)
        {R = 1.0
        G = -(w - 645) / (645 - 580)
        B = 0.0  }
    else if (w >= 645 && w <= 780)
        {R = 1.0
        G = 0.0
        B = 0.0 }
    else
        {R = 0.0
        G = 0.0
        B = 0.0}

    // intensity correction
    if( w >= 380 && w < 420)
        SSS = 0.3 + 0.7*(w - 350) / (420 - 350)
    else if (w >= 420 && w <= 700)
        SSS = 1.0
    else if(w > 700 && w <= 780)
        SSS = 0.3 + 0.7*(780 - w) / (780 - 700)
    else
        SSS = 0.0
     
     
     SSS = SSS * 255


     R = int(SSS * R)
     G = int(SSS * G)
     B = int(SSS * B)
     
     comp = R + (256 * G) + (65536 * B)
     
     outfile.WriteLine(w + "\t" +  R + "\t" + G + "\t" + B + "\t" + comp)
               
    }
 
    outfile.Close()
}

main()